using Microsoft.EntityFrameworkCore;
using PocketPals.Models;

namespace PocketPals.Data
{
    /// <summary>
    /// 应用程序数据库上下文
    /// </summary>
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        /// <summary>
        /// 交易记录表
        /// </summary>
        public DbSet<Transaction> Transactions { get; set; }

        /// <summary>
        /// 余额表
        /// </summary>
        public DbSet<Balance> Balances { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // 配置Transaction实体
            modelBuilder.Entity<Transaction>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Amount).HasColumnType("decimal(18,2)");
                entity.Property(e => e.TransactionTime).IsRequired();
                entity.Property(e => e.Type).IsRequired();
            });

            // 配置Balance实体
            modelBuilder.Entity<Balance>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Amount).HasColumnType("decimal(18,2)");
                entity.Property(e => e.OperationAmount).HasColumnType("decimal(18,2)");
                entity.Property(e => e.BeforeAmount).HasColumnType("decimal(18,2)");
                entity.Property(e => e.AfterAmount).HasColumnType("decimal(18,2)");
            });
        }
    }
}